package com.walterlife.javacore;

import com.sun.xml.internal.bind.v2.runtime.reflect.opt.OptimizedAccessorFactory;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Optional;

/**
 * @author zhuyuwei
 * @date 2017/6/20
 * @description: JDK8 feature
 */
public class Jdk8Test {

  public static void main(String[] args) {
/*    lambdaTest();
    localDateTimeTest();
    zoneDateTimeTest();*/
//    durationTest();
  }

  public Jdk8Test() {
    super();
  }

  public static void lambdaTest() {
    Arrays.asList("a", "b", "c").forEach(e -> {
      System.out.println(e);
    });

    Optional<String> fullName = Optional.ofNullable(null);
    fullName = Optional.ofNullable("Hello");
    System.out.println(fullName.orElseGet(() -> "[none]"));
    System.out.println(fullName.map(s -> "Hey " + s + "!").orElse(" Hey Stronger"));
  }

  public static void localDateTimeTest() {
    // LocalDateTime
    LocalDateTime localDateTime = LocalDateTime.now();
    System.out.println(localDateTime.toLocalDate().toString());

    localDateTime = LocalDateTime.of(2017, 6, 19, 10, 20);
    System.out.println(localDateTime.toLocalTime().toString());

    System.out.println(localDateTime.getDayOfYear());
    System.out.println(localDateTime.getDayOfMonth());
    System.out.println(localDateTime.getDayOfWeek().getValue());

    System.out.println(localDateTime.plusHours(1));
    System.out.println(localDateTime.plusMonths(1));
    localDateTime = localDateTime.plusMonths(1);
    System.out.println(localDateTime.toLocalDate().toString());

    // DateTimeFormatter
    System.out.println(localDateTime.plusDays(1)
        .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

    // 设置某月的第几天
    System.out.println(localDateTime.withDayOfMonth(10));
    // 设置某年的第几天
    System.out.println(localDateTime.withDayOfYear(10));
  }

  // ZoneDateTime
  public static void zoneDateTimeTest() {
    // ZoneDateTime
    ZonedDateTime zonedDateTime = ZonedDateTime.now();
    System.out.println(zonedDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
    System.out.println(zonedDateTime);
    zonedDateTime = ZonedDateTime.now(ZoneId.of("Asia/Shanghai"));
    System.out.println(zonedDateTime);
  }

  // Duration
  public static void durationTest() {
    LocalDateTime from = LocalDateTime.of(2017, 6, 12, 11, 11);
    LocalDateTime to = LocalDateTime.of(2017, 6, 12, 11, 20);
    System.out.println(Duration.between(from, to).toMinutes());


    double a = 1.11111111111111;
    double b = 1.11111111111111;
    System.out.println(a = b);
  }
}
